/*给单选框或复选框添加样式*/
function checkbox($this){
    if($this.attr('type')=="checkbox"){
        if ($this.attr('checked')=="checked") {
            $this.removeAttr("checked");
            $this.next('img').attr("src","/static/images/c_checkbox_off.png");
        }else{
            $this.attr("checked","checked");
            $this.next('img').attr("src","/static/images/c_checkbox_on.png");
        }
    }
}
//小图表箭头-展开
function addDiyDom(treeId, treeNode) {
    var spaceWidth = 5;
    var switchObj = $("#" + treeNode.tId + "_switch");
    icoObj = $("#" + treeNode.tId + "_ico");
    switchObj.remove();
    icoObj.before(switchObj);
    if (treeNode.level > 1) {
        //var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level)+ "px'></span>";
        var spaceStr = "<span style='display: inline-block;width:5px'></span>";
        switchObj.before(spaceStr);
    }

}
//数据读取 初始化
$.ajax({
    url:"http://127.0.0.1:8020/html/zizhisystem/node.json",
    type : "get",
    dataType :"text",
    success: function(data){

        var treeObj = $("#treeDemo");
        $.fn.zTree.init(treeObj, setting, arr);
    },
    error: function(){

    }
});

//回调
function zTreeOnClick(event, treeId, treeNode) {
    var treeObj=$.fn.zTree.getZTreeObj("treeDemo"),
        nodes=treeObj.getSelectedNodes(true),  //获取光标选中的值(selected)
        v="";
    for(var i=0;i<nodes.length;i++){
        v=nodes[i].name + ",";
        console.log("节点的id:"+nodes[i].id+"节点的名称:"+nodes[i].name); //获取选中节点的值
    }
};
//关闭弹窗-layer
function closedHandler(){
    var index = parent.layer.getFrameIndex(window.name);

    //关闭弹出层
    parent.layer.close(index);
}

var allId = []
$(function(){
    //上游全选
    $('.editAllImg').click(function(){
        var $list_input=$(this).parents('.editup_head').next('.editup_body').find('input[type=checkbox]'); //找到子类的按钮
        var idAll =[];
        if ($(this).attr("checked")==undefined || $(this).attr("checked")=="") {
            $list_input.attr("checked","checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_on.png");
            $(".editup_body input[type='checkbox']").each(function() {
                var id = $(this).attr('data-id')
                idAll.push(id);
                allId=idAll
            })
            console.log(allId)
        }else{
            $list_input.removeAttr("checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_off.png");
            allId=[];

        }
        console.log(allId)
        checkbox($(this));
    })
    //单选
    $("body").on("change",".editup_body input[type='checkbox']",function(){

        var $ul_input=$(this).parents('.editup_body').prev('.editup_head').find('input');
        if($(this).parents('.editup_head').find("input[checked='checked']").length== $('.checkbox_child').length){
            $ul_input.prop("checked","checked");
            $ul_input.next('img').attr("src","/static/images/c_checkbox_on.png");
        }else{
            $ul_input.prop("checked",false);
            $ul_input.next('img').attr("src","/static/images/c_checkbox_off.png");
        }

        if($(this).attr("checked")=="" || $(this).attr("checked")==undefined){
            $(this).attr("checked",true);
            $(this).next('img').attr("src","/static/images/c_checkbox_on.png");
            var idAll =[];
            $(".editup_body input[type='checkbox']:checked").each(function(){
                var id = $(this).attr("data-id");
                console.log('正选')
                idAll.push(id);
                allId=idAll
            })

            console.log(allId)


        }else{
            $(this).attr("checked",false);
            $(this).next('img').attr("src","/static/images/c_checkbox_off.png");
            var id = $(this).attr("data-id");
            var index =  allId.indexOf(id);
            console.log(index)
            if(index>-1){
                allId.splice(index, 1);
            }else{
                allId=[]
            }
            console.log(allId)

        }
    })

})

//关闭弹窗-layer
function closedHandler(){
    var index = parent.layer.getFrameIndex(window.name);

    //关闭弹出层
    parent.layer.close(index);
}

$(function(){
    //下游全选
    $('.downAllImg').click(function(){
        var $list_input=$(this).parents('.edown_head').next('.edown_body').find('input[type=checkbox]'); //找到子类的按钮
        var idAll =[];
        if ($(this).attr("checked")==undefined || $(this).attr("checked")=="") {
            $list_input.attr("checked","checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_on.png");
            $(".edown_body input[type='checkbox']").each(function() {
                var id = $(this).attr('data-id')
                idAll.push(id);
                allId=idAll
            })
            console.log(allId)
        }else{
            $list_input.removeAttr("checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_off.png");
            allId=[];

        }
        console.log(allId)
        checkbox($(this));
    })
    //单选
    $("body").on("change",".edown_body input[type='checkbox']",function(){

        var $ul_input=$(this).parents('.edown_body').prev('.edown_head').find('input');
        if($(this).parents('.edown_head').find("input[checked='checked']").length== $('.checkbox_child').length){
            $ul_input.prop("checked","checked");
            $ul_input.next('img').attr("src","/static/images/c_checkbox_on.png");
        }else{
            $ul_input.prop("checked",false);
            $ul_input.next('img').attr("src","/static/images/c_checkbox_off.png");
        }

        if($(this).attr("checked")=="" || $(this).attr("checked")==undefined){
            $(this).attr("checked",true);
            $(this).next('img').attr("src","/static/images/c_checkbox_on.png");
            var idAll =[];
            $(".edown_body input[type='checkbox']:checked").each(function(){
                var id = $(this).attr("data-id");
                console.log('正选')
                idAll.push(id);
                allId=idAll
            })

            console.log(allId)


        }else{
            $(this).attr("checked",false);
            $(this).next('img').attr("src","/static/images/c_checkbox_off.png");
            var id = $(this).attr("data-id");
            var index =  allId.indexOf(id);
            console.log(index)
            if(index>-1){
                allId.splice(index, 1);
            }else{
                allId=[]
            }
            console.log(allId)

        }
    })

})

//弹簧全选
var allId = []
$(function(){
    //全选
    $('.protCheckAll').click(function(){
        var $list_input=$(this).parents('.prot_head').next('.prot_body').find('input[type=checkbox]'); //找到子类的按钮
        var idAll =[];
        if ($(this).attr("checked")==undefined || $(this).attr("checked")=="") {
            $list_input.attr("checked","checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_on.png");
            $(".prot_body input[type='checkbox']").each(function() {
                var id = $(this).attr('data-id')
                idAll.push(id);
                allId=idAll
            })
            console.log(allId)
        }else{
            $list_input.removeAttr("checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_off.png");
            allId=[];

        }
        console.log(allId)
        checkbox($(this));
    })
    //单选
    $("body").on("change",".prot_body input[type='checkbox']",function(){

        var $ul_input=$(this).parents('.prot_body').prev('.tbhead').find('input');
        if($(this).parents('.tbhead').find("input[checked='checked']").length== $('.checkbox_child').length){
            $ul_input.prop("checked","checked");
            $ul_input.next('img').attr("src","/static/images/c_checkbox_on.png");
        }else{
            $ul_input.prop("checked",false);
            $ul_input.next('img').attr("src","/static/images/c_checkbox_off.png");
        }

        if($(this).attr("checked")=="" || $(this).attr("checked")==undefined){
            $(this).attr("checked",true);
            $(this).next('img').attr("src","/static/images/c_checkbox_on.png");
            var idAll =[];
            $(".prot_body input[type='checkbox']:checked").each(function(){
                var id = $(this).attr("data-id");
                console.log('正选')
                idAll.push(id);
                allId=idAll
            })

            console.log(allId)


        }else{
            $(this).attr("checked",false);
            $(this).next('img').attr("src","/static/images/c_checkbox_off.png");
            var id = $(this).attr("data-id");
            var index =  allId.indexOf(id);
            console.log(index)
            if(index>-1){
                allId.splice(index, 1);
            }else{
                allId=[]
            }
            console.log(allId)

        }
    })

})
$(function(){
    //全部商品-弹窗
    $('#sampled_wadd').on('click', function(){
        layer.open({
            type:1,
            title: '选择商品',
            area:['850px','700px'],
            btn:["确定","取消"],
            scrollbar: false,
            offset: "10px",
            //shadeClose:true,
            content:$('.j-mproChoose'),

            yes:function(){
                console.log(allId)
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                $.ajax({
                    type: "POST",
                    url: "/dangerous/dangerous/dangerous_upstream",
                    dataType: 'json',
                    data: {allId: allId},
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function (data) {
                        console.log(data)
                        if(data.status==1){
                            var tr = $(".choosed_body").empty();
                            var addertr_up = $("#addertr");
                            for (var i = 0; i < data.dataList.length; i++) {
                                var str = '<tr><td>'+data.dataList[i].goods_id+'</td><td>'+data.dataList[i].goods_name+'</td>';
                                var addertr_up_str = '<tr><td class="linhigh"><label class="lbmargin"><input type="checkbox" data-id='+data.dataList[i].goods_id+' class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png/"/></label></td><td class="linhigh">'+i+'</td><td class="linhigh">'+data.dataList[i].goods_id+'</td><td class="linhigh">'+data.dataList[i].original_bn+'</td><td class="linhigh">'+data.dataList[i].goods_name+'</td><td class="linhigh">'+data.dataList[i].cas+'</td></tr>'
                                tr.append(str);
                                addertr_up.append(addertr_up_str);
                                console.log(addertr_up_str)
                            }

                            layer.msg('上游产品新增成功',{icon:1,time:1500,offset:"50%"});
                            closedHandler()


                        }else{
                            layer.msg(data.msg,{icon:2,time:1500,offset:"50%"});

                        }

                    }

                });

            },


        });
    });
    // //模糊查询demo-web
    // $("#j-subSeachBtn").on("click",function(){
    // 	var val = $.trim($(".j-serchput").val());
    // 	if(val != ""){
    // 		$("#serchTable tr:not('.hedtr')").hide().filter(":contains('"+val+"')").show();
    // 	}
    // })
//    下游商品添加
    $('#sampled_downLowerAdd').on('click', function(){
        layer.open({
            type:1,
            title: '选择商品',
            area:['850px','700px'],
            btn:["确定","取消"],
            scrollbar: false,
            offset: "10px",
            //shadeClose:true,
            content:$('.j-mproChoose'),

            yes:function(){
                console.log(allId)
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                $.ajax({
                    type: "POST",
                    url: "/dangerous/dangerous/dangerous_downstream",
                    dataType: 'json',
                    data: {allId: allId},
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function (data) {
                        console.log(data)
                        if(data.status==1){
                            var tr = $(".choosed_body").empty();
                            var addertr_down = $("#down_addertr");
                            for (var i = 0; i < data.dataList.length; i++) {
                                var str = '<tr><td>'+data.dataList[i].goods_id+'</td><td>'+data.dataList[i].goods_name+'</td>';
                                var addertr_down_str = '<tr><td class="linhigh"><label class="lbmargin"><input type="checkbox" data-id='+data.dataList[i].goods_id+' class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png/"/></label></td><td class="linhigh">'+i+'</td><td class="linhigh">'+data.dataList[i].goods_id+'</td><td class="linhigh">'+data.dataList[i].original_bn+'</td><td class="linhigh">'+data.dataList[i].goods_name+'</td><td class="linhigh">'+data.dataList[i].cas+'</td></tr>'
                                tr.append(str);
                                addertr_down.append(addertr_down_str);
                                console.log(str)
                            }

                            layer.msg('下游产品新增成功',{icon:1,time:1500,offset:"50%"});



                        }else{
                            layer.msg(data.msg,{icon:2,time:1500,offset:"50%"});
                        }

                    }

                });

            },


        });
    });

});
//删除事件
$(function(){
    $("body").on("click","#sample_delpro",function(){
        //
        console.log(allId)
        if(allId.length==0){
            layer.msg('请选择您要删除产品',{icon:2,time:1500});
            return;
        }else{
            $(".editup_body input[type='checkbox']:checked").each(function() {
                $(this).parents("tr").remove();

            })
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/dangerous/dangerous/dangerous_up_delete",
                dataType: 'json',
                data: {allId: allId},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function (data) {

                    if(data.status==1){

                        layer.msg(data.msg,{icon:1,time:1500,offset:"50%"});



                    }else{
                        layer.msg(data.msg,{icon:2,time:1500,offset:"50%"});
                    }

                }

            });
        }


    })



        $("body").on("click","#sample_downLowerDel",function(){
       console.log(allId.length)
        if(allId.length==0){
            layer.msg('请选择您要删除产品',{icon:2,time:1500,offset:"50%"});
            return;
        }else {


            $(".tup_body input[type='checkbox']:checked").each(function () {
                $(this).parents("tr").remove();

            })
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/dangerous/dangerous/dangerous_down_delete",
                dataType: 'json',
                data: {allId: allId},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function (data) {

                    if (data.status == 1) {

                        layer.msg(data.msg, {icon: 1, time: 1500,offset:"50%"});


                    } else {
                        layer.msg(data.msg, {icon: 2, time: 1500,offset:"50%"});
                    }

                }

            });
        }
    })


})